package com.lmc.chatroom.controller;

import com.lmc.chatroom.constant.constant;
import com.lmc.chatroom.model.Result;
import com.lmc.chatroom.model.User;
import com.lmc.chatroom.service.userService;
import com.lmc.chatroom.utils.JwtUtil;
import com.lmc.chatroom.utils.SecurityUtil;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    userService userService;

    @RequestMapping("/register")
    public Result register(String username, String password) {
        log.info("register");
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return Result.fail("用户名或密码不能为空");
        }
        try {
            if(userService.register(username, password)){
                return Result.success(true);
            }else{
                return Result.fail("注册失败，请检查用户名或密码");
            }
        } catch (DuplicateKeyException e) {
            log.info("注册失败,用户名重复");
            return Result.fail("注册失败，用户名已存在，请更换");
        }
    }

    @RequestMapping("/login")
    public Result login(String username, String password, HttpServletRequest request) {
        log.info("login");
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            return Result.fail("用户名或密码不能为空");
        }

        User user = userService.selectByUsername(username);
        if(user == null){
            return Result.fail("没有此用户");
        }
        if(!SecurityUtil.checkPassword(password, user.getPassword())){
            return Result.fail("密码错误");
        }
        user.setPassword(null);
        log.info("login success"+user);
        HttpSession session = request.getSession(true);
        session.setAttribute("user", user);
//        生成并返回token
        Map<String,Object> clain = new HashMap<>();
        clain.put(constant.USER_CLAIM_ID, user.getUsername());
        clain.put(constant.USER_CLAIM_NAME, user.getUsername());
        String token = JwtUtil.getToken(clain);
        log.info("login success"+token);
        return Result.success(token);

    }

    @RequestMapping("/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        log.info("getUserInfo");
        HttpSession session = request.getSession(false);
        if(session == null){
            log.info("客户端页面获取session失败，session为空");
            return Result.fail("获取用户信息失败");
        }
        User user = (User) session.getAttribute("user");
        if(user == null){
            log.info("客户端页面获取用户信息失败，user为空");
            return Result.fail("获取用户信息失败");
        }
//        过滤多余信息
        user.setCreateTime(null);
        user.setUpdateTime(null);
        user.setDeleteFlag(null);
        return Result.success(user);
    }



}
